﻿using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Shuba.Model;

namespace Shuba.Data
{
    public class Menu
    {
        public Menu()
        { }

        public List<MenuInfo> GetParentMenuList(int UserID, string AppCode)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            //Database db = DatabaseFactory.CreateDatabase();
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand("EXEC GET_PARENT_MENU_BY_USER @APP_ID, @USER_ID");
            db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
            db.AddInParameter(cmd, "@USER_ID", DbType.Int32, UserID);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(new MenuInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }
                dr.Close();
            }
            return list;
        }

        public List<MenuInfo> GetParentMenuList(string AppCode, int UserID, string UserType)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            //Database db = DatabaseFactory.CreateDatabase();
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand("EXEC GET_PARENT_MENU_BY_USER_FOR_ALTERNATE @APP_ID, @USER_ID, @USER_TYPE");
            db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
            db.AddInParameter(cmd, "@USER_ID", DbType.Int32, UserID);
            db.AddInParameter(cmd, "@USER_TYPE", DbType.String, UserType);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(new MenuInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }
                dr.Close();
            }
            return list;
        }

        public List<MenuInfo> GetSubMenuList(int MenuID, int UserID, string AppCode)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            //Database db = DatabaseFactory.CreateDatabase();
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand("EXEC GET_SUB_MENU_BY_USER @APP_ID, @PARENT_MENU_ID, @USER_ID");
            db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode); 
            db.AddInParameter(cmd, "@PARENT_MENU_ID", DbType.Int32, MenuID); 
            db.AddInParameter(cmd, "@USER_ID", DbType.Int32, UserID);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(new MenuInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }
                dr.Close();
            }
            return list;
        }

        public List<MenuInfo> GetSubMenuList(int MenuID, int UserID, string AppCode, string UserType)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand("EXEC GET_SUB_MENU_BY_USER_FOR_ALTERNATE @APP_ID, @PARENT_MENU_ID, @USER_ID, @USER_TYPE");
            db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
            db.AddInParameter(cmd, "@PARENT_MENU_ID", DbType.Int32, MenuID);
            db.AddInParameter(cmd, "@USER_ID", DbType.Int32, UserID);
            db.AddInParameter(cmd, "@USER_TYPE", DbType.String, UserType);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(new MenuInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }
                dr.Close();
            }
            return list;
        }

        public MenuInfo Get(string uniqueName, string AppCode)
        {
            MenuInfo menu = new MenuInfo();
            //Database db = DatabaseFactory.CreateDatabase();
            Database db = DBConnection.CreateDatabase();

            DbCommand cmd = db.GetSqlStringCommand("EXEC GET_MENU_NAME_BY_UNIQUE @UNIQUE_NAME, @APP_CODE");
            db.AddInParameter(cmd, "@UNIQUE_NAME", DbType.String, uniqueName);
            db.AddInParameter(cmd, "@APP_CODE", DbType.String, AppCode);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    menu = new MenuInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3));
                }
                dr.Close();
            }
            return menu;
        }
    }
}
